<?php
namespace cms\modules\v1\controllers;

use cms\modules\v1\models\access\ModuleActions;
use cms\modules\v1\models\shop\GoodsComment;
use cms\modules\v1\models\express\ExpressCompany;
use cms\modules\v1\models\shop\Goods;
use cms\modules\v1\models\shop\GoodsCategory;
use cms\modules\v1\models\shop\GoodsDetail;
use cms\modules\v1\models\shop\GoodsOrder;
use cms\modules\v1\models\shop\GoodsOrderReturn;
use common\models\Functions;
use common\models\Status;
use Yii;

class ShopController extends DefaultController
{
    public $modelClass = 'cms\modules\v1\models\goods\Goods';

    //---------------------------- 商品列表 -------------------------------
    //获取banner图片
    public function actionList($page = 0,$size = 10) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (\Yii::$app->request->isGet) {
            return (new Goods())->getListData($page, $size);
        }

    }

    public function actionAddEdit($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        $model = new Goods();
        if (Yii::$app->request->isGet) {
            return $model->getInfo($id);
        }

        if (Yii::$app->request->isPost) {
            $model->setScenario('addEdit');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->addEdit();
            }
        }
    }

    public function actionDelete() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isDelete) {
            $id = Yii::$app->request->get('id');
            return (new Goods())->deleteGoods($id);
        }
    }

    public function actionSwitchStatus() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new Goods())->switchStatus();
        }
    }

    public function actionMap() {
        if (\Yii::$app->request->isGet) {
            return (new Goods())->getMapForSelect();
        }
    }

    //商品审核
    public function actionExamine() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new Goods())->examineInfo();
        }
    }


    //---------------------------- 商品category -------------------------------
    public function actionCategoryList() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new GoodsCategory())->getList();
        }
    }

    public function actionCategoryAddEdit($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        $model = new GoodsCategory();
        if (Yii::$app->request->isGet) {
            $list =  $model->getInfo($id);
            return $list;
        }

        if (Yii::$app->request->isPost) {
            $model->setScenario('addEdit');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->addEdit();
            }
        }
    }

    //逻辑删除
    public function actionCategoryDelete() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isDelete) {
            $id = Yii::$app->request->get('id',0);
            return (new GoodsCategory())->deleteInfo($id);
        }
    }

    public function actionCategoryOrder() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new GoodsCategory())->saveOrder();
        }
    }

    public function actionCategorySwitchStatus() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new GoodsCategory())->switchStatus();
        }
    }

    //获取树形map 已经是否选取了产品
    public function actionCategoryTree($goodsId = '') {
        if (Yii::$app->request->isGet) {
            return (new GoodsCategory())->getTree($goodsId);
        }
    }

    //用于搜索的特殊MAP
    public function actionCategorySpecialMap() {
        if (Yii::$app->request->isGet) {
            return (new GoodsCategory())->getSpecialTreeMap();
        }
    }


    public function actionCategoryMap() {
        if (\Yii::$app->request->isGet) {
            return (new GoodsCategory())->getCategoryMap();
        }
    }


    /* ====================== 商品订单管理 ========================= */
    //列表
    public function actionGoodsOrderList($page = 1, $size = 20){
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new GoodsOrder())->getListData($page, $size);
        }
    }

    public function actionGoodsOrderStatusMap () {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return Functions::formatRestAPI(0,'',Status::USER_GOODS_ORDER_STATUS_MAP);
        }
    }

    /**
     * 发送快递
     * @return mixed
     */
    public function actionGoodsOrderDeliver() {
        if (\Yii::$app->request->isPost) {
            return (new GoodsOrder())->deliver();
        }
    }

    /**
     * 获取退款信息
     * @param int $id
     * @return array
     */
    public function actionOrderReturn($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new GoodsOrderReturn())->getInfo($id);
        }
    }

    /**
     * 同意或驳回申请
     * @return array
     */
    public function actionEditReturn() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new GoodsOrderReturn())->addEdit();
        }
    }

    //查看详情
    public function actionGoodsOrderInfo($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new GoodsOrder())->getInfo($id);
        }
    }

    //删除订单
    public function actionGoodsOrderDelete($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (\Yii::$app->request->isDelete) {
            return (new GoodsOrder())->goodsOrderDelete($id);
        }
    }

    //后台取消订单
    public function actionOrderCancel() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (\Yii::$app->request->isPost) {
            return (new GoodsOrder())->orderCancel();
        }
    }

    /* ====================== 商品评论管理 ========================= */

    //评论列表
    public function actionCommentList($page = 1, $size = 20){
        if (Yii::$app->request->isGet) {
            return (new GoodsComment())->getListData($page, $size);
        }
    }

    //删除评论
    public function actionCommentDel() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new GoodsComment())->getDelete();
        }
    }
    //审核提示音
    public function actionCommentAudit() {
        if (Yii::$app->request->isGet) {
            return (new GoodsComment())->queryAudit();
        }
    }

    //评论审核
    public function actionCommentExamine() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new GoodsComment())->saveExamine();
        }
    }

}
